#include <stdio.h>
#include <libpq-fe.h>

int main() {
    // 连接参数（根据实际情况修改）
    const char *host = "localhost";
    const char *port = "db_port";
    const char *dbname = "db_name";
    const char *user = "db_user";
    const char *password = "user_passwd";

    // 建立数据库连接
    PGconn *conn = PQsetdbLogin(host, port, NULL, NULL, dbname, user, password);

    // 检查连接状态
    if (PQstatus(conn) != CONNECTION_OK) {
        fprintf(stderr, "Failed to connect to database: %s", PQerrorMessage(conn));
        PQfinish(conn);
        return 1;
    }
    printf("Successfully connected to the database！\n");

    // 关闭连接
    PQfinish(conn);
    return 0;
}